package com.ruoyi.lab.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lab.domain.LabSeatMessage;
import com.ruoyi.lab.enums.AppointStatusEnum;
import com.ruoyi.lab.service.ILabSeatMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lab.mapper.LabAppointMessageMapper;
import com.ruoyi.lab.domain.LabAppointMessage;
import com.ruoyi.lab.service.ILabAppointMessageService;

/**
 * 学生预约信息Service业务层处理
 * 
 * @author dn
 * @date 2023-12-31
 */
@Service
@Slf4j
public class LabAppointMessageServiceImpl implements ILabAppointMessageService 
{
    @Autowired
    private LabAppointMessageMapper labAppointMessageMapper;

    /**
     * 查询学生预约信息
     * 
     * @param appointId 学生预约信息主键
     * @return 学生预约信息
     */
    @Override
    public LabAppointMessage selectLabAppointMessageByAppointId(Long appointId)
    {
        return labAppointMessageMapper.selectLabAppointMessageByAppointId(appointId);
    }

    /**
     * 查询学生预约信息列表
     * 
     * @param labAppointMessage 学生预约信息
     * @return 学生预约信息
     */
    @Override
    public List<LabAppointMessage> selectLabAppointMessageList(LabAppointMessage labAppointMessage)
    {
        List<LabAppointMessage> list = labAppointMessageMapper.selectLabAppointMessageList(labAppointMessage);
        for (LabAppointMessage appointMessage : list) {
            Date startAppointTime = appointMessage.getStartAppointTime();
            Date endAppointTime = appointMessage.getEndAppointTime();

            if (DateUtils.getNowDate().compareTo(endAppointTime) >= 0) {
                // 如果当前时间已过了预约截止时间，则状态修改为 已结束
                appointMessage.setAppointStatus(AppointStatusEnum.APPOINT_END.getStatus());
                labAppointMessageMapper.updateLabAppointMessage(appointMessage);
            }
            else if (DateUtils.getNowDate().compareTo(startAppointTime) >= 0) {
                // 如果当前时间超过了预约开始时间，且未到截止时间，则状态修改为 使用中
                appointMessage.setAppointStatus(AppointStatusEnum.APPOINT_START.getStatus());
                labAppointMessageMapper.updateLabAppointMessage(appointMessage);
            }
        }
        return labAppointMessageMapper.selectLabAppointMessageList(labAppointMessage);
    }

    /**
     * 新增学生预约信息
     * 
     * @param labAppointMessage 学生预约信息
     * @return 结果
     */
    @Override
    public int insertLabAppointMessage(LabAppointMessage labAppointMessage)
    {
        if (!canAppoint(labAppointMessage)) {
            return -1;
        }

        labAppointMessage.setAppointId(System.currentTimeMillis());
        labAppointMessage.setStudentWorkno(SecurityUtils.getUsername());
        labAppointMessage.setCreateBy(SecurityUtils.getUsername());
        labAppointMessage.setCreateTime(DateUtils.getNowDate());
        labAppointMessage.setAppointStatus(AppointStatusEnum.APPOINT_READY.getStatus()); // 若可预约，则状态为 未开始状态
        return labAppointMessageMapper.insertLabAppointMessage(labAppointMessage);
    }

    /**
     * 修改学生预约信息
     * 
     * @param labAppointMessage 学生预约信息
     * @return 结果
     */
    @Override
    public int updateLabAppointMessage(LabAppointMessage labAppointMessage)
    {
        if (!canAppoint(labAppointMessage)) {
            return -1;
        }

        // 如果已经开始或已经结束，则不能更改
        Long appointId = labAppointMessage.getAppointId();
        if (appointId == null) {
            log.error("预约信息id不能为空");
            return -1;
        }
        LabAppointMessage appointMessage = labAppointMessageMapper.selectLabAppointMessageByAppointId(appointId);
        if (!AppointStatusEnum.APPOINT_READY.getStatus().equals(appointMessage.getAppointStatus())) {
            log.error("已经开始或已经结束，不能修改预约信息");
            return -1;
        }

        labAppointMessage.setUpdateBy(SecurityUtils.getUsername());
        labAppointMessage.setUpdateTime(DateUtils.getNowDate());
        return labAppointMessageMapper.updateLabAppointMessage(labAppointMessage);
    }

    /**
     * 批量删除学生预约信息
     * 
     * @param appointIds 需要删除的学生预约信息主键
     * @return 结果
     */
    @Override
    public int deleteLabAppointMessageByAppointIds(Long[] appointIds)
    {
        // 如果已经开始或已经结束，则不能取消
        for (Long appointId : appointIds) {
            LabAppointMessage appointMessage = labAppointMessageMapper.selectLabAppointMessageByAppointId(appointId);
            if (!AppointStatusEnum.APPOINT_READY.getStatus().equals(appointMessage.getAppointStatus())) {
                log.error("已经开始或已经结束，不能修改预约信息");
                return -1;
            }
        }
        return labAppointMessageMapper.deleteLabAppointMessageByAppointIds(appointIds);
    }

    /**
     * 删除学生预约信息信息
     * 
     * @param appointId 学生预约信息主键
     * @return 结果
     */
    @Override
    public int deleteLabAppointMessageByAppointId(Long appointId)
    {
        // 如果已经开始或已经结束，则不能取消
        LabAppointMessage appointMessage = labAppointMessageMapper.selectLabAppointMessageByAppointId(appointId);
        if (!AppointStatusEnum.APPOINT_READY.getStatus().equals(appointMessage.getAppointStatus())) {
            log.error("已经开始或已经结束，不能取消预约信息");
            return -1;
        }
        return labAppointMessageMapper.deleteLabAppointMessageByAppointId(appointId);
    }

    /**
     * 判断能否预约
     * @param labAppointMessage
     * @return
     */
    private Boolean canAppoint(LabAppointMessage labAppointMessage) {
        Date startAppointTime = labAppointMessage.getStartAppointTime();
        Date endAppointTime = labAppointMessage.getEndAppointTime();
        if (startAppointTime == null || endAppointTime == null) {
            log.error("预约起止时间不能为空");
            return false;
        }
        if (endAppointTime.compareTo(startAppointTime) <= 0) {
            log.error("开始时间不能小于结束时间");
            return false;
        }

        Long seatId = labAppointMessage.getSeatId();
        if (seatId == null) {
            log.error("预约座位id不能为空");
            return false;
        }

        // 根据座位id查询跟这个座位相关的所有预约信息
        List<LabAppointMessage> list = labAppointMessageMapper.selectLabAppointMessageList(labAppointMessage);
        for (LabAppointMessage appointMessage : list) {
            // 如果修改自己的数据，则不需要跟自己比
            if (appointMessage.getAppointId().equals(labAppointMessage.getAppointId())) continue;

            // 判断能否预约
            Date start = appointMessage.getStartAppointTime();
            Date end = appointMessage.getEndAppointTime();
            if ((startAppointTime.compareTo(end) >= 0) || (endAppointTime.compareTo(start) <= 0)) {
                continue;
            } else {
                log.error("冲突，不能预约");
                return false;
            }
        }

        return true;
    }
}
